﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using uTools;
using DG.Tweening;
public class Level6X6Letters : MonoBehaviour
{
    public List<Transform> loveRootList = new List<Transform>();
    public List<TextButton> buttonList = new List<TextButton>();
    public List<uTweenScale> tweenList = new List<uTweenScale>();
    private List<int> loveIndexList = new List<int>();
    private List<string> letters = new List<string>();
    public Color activeColor;
    public Color inactiveColor;
    public Image bgImage;

    private int activeBtnCount = 4;
    public GameLevelItem gameLevelItem;
    void Start()
    {
        loveIndexList.Add(18);
        loveIndexList.Add(1);
        loveIndexList.Add(33);
        loveIndexList.Add(17);
        InitLetters();
        InitButton();
    }

    private void InitButton()
    {
        int count = buttonList.Count;
        if (count != letters.Count)
            Debug.LogError("字母和按钮的数量不匹配");
        for (int i = 0; i < count; i++)
        {
            int index = i;
            buttonList[index].Init(letters[index], () =>
            {
                OnButtonPress(index);
            });
        }
    }

    private void OnButtonPress(int index)
    {
        ChangeButtonColor(index, true);
        List<int> indexList = GetNeedInactiveButtonIndexs(index);
        if (null != indexList)
        {
            int count = indexList.Count;
            for (int i = 0; i < count; i++)
            {
                ChangeButtonColor(indexList[i], false);
            }
        }
        JudgeResult();
    }

    private List<int> GetNeedInactiveButtonIndexs(int index)
    {
        // 0  1  2  3  4  5
        // 6  7  8  9  10 11
        // 12 13 14 15 16 17
        // 18 19 20 21 22 23
        // 24 25 26 27 28 29
        // 30 31 32 33 34 35
        List<int> indexList = new List<int>();
        int row = 0;            // 行
        int cloumn = 0;         // 列
        row = index / 6;
        cloumn = index % 6;
        int length = letters.Count;
        for (int i = 0; i < length; i++)
        {
            if (i != index)
            {
                if (i / 6 == row)
                    indexList.Add(i);
                else if (i % 6 == cloumn)
                    indexList.Add(i);
            }
        }

        for (int i = 0; i < indexList.Count; i++)
        {
            Debug.Log(indexList[i]);
        }

        return indexList;
    }

    private void InitLetters()
    {
        letters.Clear();
        letters.Add("c");   //0
        letters.Add("o");   //1  OO
        letters.Add("r");   //2
        letters.Add("b");   //3
        letters.Add("t");   //4
        letters.Add("h");   //5
        letters.Add("a");   //6
        letters.Add("f");   //7
        letters.Add("0");   //8
        letters.Add("m");//9
        letters.Add("g");//10
        letters.Add("2");//11
        letters.Add("n");//12
        letters.Add("k");//13
        letters.Add("i");//14
        letters.Add("s");//15
        letters.Add("q");//16
        letters.Add("e");//17   EEE
        letters.Add("l");//18  LLL
        letters.Add("u");//19
        letters.Add("4");//20
        letters.Add("x");//21
        letters.Add("9");//22
        letters.Add("y");//23
        letters.Add("p");//24
        letters.Add("w");//25
        letters.Add("3");//26
        letters.Add("z");//27
        letters.Add("8");//28
        letters.Add("5");//29
        letters.Add("c");//30
        letters.Add("7");//31
        letters.Add("a");//32
        letters.Add("v");//33   VVV
        letters.Add("b");//34
        letters.Add("d");//35
    }

    private void ChangeButtonColor(int index, bool active)
    {
        buttonList[index].button.image.color = active ? activeColor : inactiveColor;
    }

    private void JudgeResult()
    {
        bool result = true;
        int activeBtnCount = 0;
        int length = buttonList.Count;
        for (int i = 0; i < length; i++)
        {
            if (buttonList[i].button.image.color == activeColor)
            {
                activeBtnCount++;
                if (!UConst.Str_TargetLove.Contains(buttonList[i].text.text))
                {
                    result = false;
                    break;
                }
            }

        }
        if (activeBtnCount != 4)
        {
            result = false;
        }
        if (result)
        {
            for (int i = 0; i < length; i++)
            {
                if (buttonList[i].button.image.color != activeColor)
                {
                    tweenList[i].Play(PlayDirection.Forward);
                }
            }
            bgImage.DOFade(0, 0.3f);

            MoveToRightPos();
        }
    }

    private void MoveToRightPos()
    {
        int count = loveIndexList.Count;
        for (int i = 0; i < count; i++)
        {
            Transform rightTsf = buttonList[loveIndexList[i]].transform;
            rightTsf.parent = this.transform;
            rightTsf.DOLocalMove(loveRootList[i].transform.localPosition, 0.5f);
        }

        Invoke("TriggerResult", 0.8f);

    }

    private void TriggerResult()
    {
        gameLevelItem.TriggerResult(true);
    }

}
